<?php  session_start(); ?>

<?php
include "Connect.php";

$enterRecord = @$_POST['enterRecord'];

$select = @$_POST['select'];
$rN = @trim(mysql_real_escape_string($_POST['recordName']));
$recordName = @ucfirst($rN);
$recordPrice = @mysql_real_escape_string(trim($_POST['recordPrice']));


if(isset($enterRecord)){
  
if($select=="---Select record type---" || $recordName == "Record Name:" || $recordPrice == "Record Price:"){

$_SESSION['mm'] = "Please Enter all fields!!";

$_SESSION['xx'] = "";

switch ($_SESSION['hG']) {
case "food":
showFood();
break;
case "drinks":
showDrinks();
break;
case "snacks":
showSnacks();
break;
case "services":
showServices();
break;

}

 }else{
  
if($select=="Food"){

$cntr = rand(0,1000);

//mysql_num_rows(mysql_query("SELECT * FROM food"))+ 1;

$Qw = mysql_query("SELECT * FROM food WHERE Name = '$recordName' ") or die(mysql_error());

$k = mysql_fetch_array($Qw);

//echo $k['Name'];

if(mysql_num_rows($Qw)>0){

$_SESSION['xxp'] = $recordName." already exists in the database!!";

//$msg = $recordName." already exists in the database!!"; 
//header("location:dataColl.php?poll=$msg");
//include "dataColl.php";
showFood();

}else{

$s  = mysql_query("INSERT INTO food VALUES($cntr,'$recordName','$recordPrice')") or die(mysql_error());
showFood();
//include "dataColl.php";

}

  }
  
if($select=="Drinks"){

//$cntr = mysql_num_rows(mysql_query("SELECT * FROM drinks"))+ 1;

$cntr = rand(0,1000);

$Qw = mysql_query("SELECT * FROM drinks WHERE Name = '$recordName' ") or die(mysql_error());

$k = mysql_fetch_array($Qw);

//echo $k['Name'];

if(mysql_num_rows($Qw)>0){

$_SESSION['xxp'] = $recordName." already exists in the database!!";

showDrinks();
//include "dataColl.php";
//$msg =  $recordName." already exists in the database!!";
//header("location:dataColl.php?poll=$msg");

}else{

$s  = mysql_query("INSERT INTO drinks VALUES($cntr,'$recordName','$recordPrice')") or die(mysql_error());
showDrinks();
//include "dataColl.php";

}

/*
$cntr = mysql_num_rows(mysql_query("SELECT * FROM drinks"))+ 1;
mysql_query("INSERT INTO drinks VALUES($cntr,'$recordName','$recordPrice')") or die ("Drink Entry Failed, Please try again!!");
showDrinks();*/

  }
if($select=="Snacks"){


$cntr = rand(0,1000);

//$cntr = mysql_num_rows(mysql_query("SELECT * FROM snacks"))+ 1;

$Qw = mysql_query("SELECT * FROM snacks WHERE Name = '$recordName' ") or die(mysql_error());

$k = mysql_fetch_array($Qw);

//echo $k['Name'];

if(mysql_num_rows($Qw)>0){

$_SESSION['xxp'] = $recordName." already exists in the database!!";

showSnacks();
//include "dataColl.php";
//$msg =  $recordName." already exists in the database!!";
//header("location:dataColl.php?poll=$msg");

}else{

$s  = mysql_query("INSERT INTO snacks VALUES($cntr,'$recordName','$recordPrice')") or die(mysql_error());
showSnacks();
//include "dataColl.php";

}

/*
$cntr = mysql_num_rows(mysql_query("SELECT * FROM snacks"))+ 1;
mysql_query("INSERT INTO snacks VALUES($cntr,'$recordName','$recordPrice')") or die ("Snack Entry Failed, Please try again!!");
showSnacks();*/
  }
if($select=="Services"){

$cntr = rand(0,1000);

//$cntr = mysql_num_rows(mysql_query("SELECT * FROM services"))+ 1;

$Qw = mysql_query("SELECT * FROM services WHERE Name = '$recordName' ") or die(mysql_error());

$k = mysql_fetch_array($Qw);

//echo $k['Name'];

if(mysql_num_rows($Qw)>0){

$_SESSION['xxp'] = $recordName." already exists in the database!!";

showServices();
//include "dataColl.php";
//$msg =  $recordName." already exists in the database!!";
//header("location:dataColl.php?poll=$msg");

}else{

$s  = mysql_query("INSERT INTO services VALUES($cntr,'$recordName','$recordPrice')") or die(mysql_error());
showServices();
//include "dataColl.php";

}

/*$cntr = mysql_num_rows(mysql_query("SELECT * FROM services"))+ 1;
mysql_query("INSERT INTO services VALUES($cntr,'$recordName','$recordPrice')") or die ("Service Entry Failed, Please try again!!");
showServices();*/
  }  
  
  else{
 
//  $msg = "Insertion Failed";
//header("location:dataColl.php?poll=$msg");

  }
  
 }
   }
   



if (isset($_GET['page']) and $_SESSION['G'] == "food"){
 $page = $_GET['page'];
 showFood();
//unset($_SESSION['G']);
     }
 
 function showFood(){

if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; 

@$pageFor = "food";
$max_results = 4;

$from = (($page * $max_results) - $max_results);
 //$getData = mysql_query("SELECT * FROM food "." LIMIT $from, $max_results") or die("Failed to Query the Food table");
//$getData = ("SELECT * FROM food "." LIMIT $from, $max_results") or die("Failed to Query the Food table");
include "Connect.php";
$getData = mysql_query("SELECT * FROM food LIMIT $from, $max_results") or die("Failed to Query the Food table");
$_SESSION['hG'] = $Clicked = "food";
$stuff = "Foods";
$G = "Food";
$_SESSION['G'] = "food";

//echo "Value=". $_SESSION['G'];

if(!mysql_num_rows($getData)>0){
$msg =  "No data found, Food table Empty";
header("location:dataColl.php?poll=$msg");
//header("location: dataColl.php?poll=$msg");
//include "dataColl.php";
}else{
//colorRow();
//$msg =  "Some data found";
//header("location: dataColl.php?poll=$msg");
include "dataColl.php";

} 
   }
if (isset($_GET['page'])  and $_SESSION['G'] == "drinks"){
$page = $_GET['page'];
showDrinks();
 }

function showDrinks(){
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; 

@$pageFor = "drinks";
$max_results = 4;

$from = (($page * $max_results) - $max_results);
$getData = mysql_query("SELECT * FROM drinks LIMIT $from, $max_results") or die("Failed to Query the Drinks table");

//$getData = mysql_query("SELECT * FROM drinks") or die("Failed to Query the Drinks table");
$_SESSION['hG'] = $Clicked="drinks";
$stuff = "Drinks";
$G = "Drink";
$_SESSION['G'] = "drinks";

if(!mysql_num_rows($getData)>0){
$msg =  "No data found, Drinks table Empty";
header("location:dataColl.php?poll=$msg");
//header("location: dataColl.php?poll=$msg");
//include "dataColl.php";
}else{
$msg =  "Some data found";
//header("location: dataColl.php?poll=$msg");
include "dataColl.php";
}

}

if (isset($_GET['page'])  and $_SESSION['G'] == "snacks"){
$page = $_GET['page'];
showSnacks();
 }

function showSnacks(){
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; 

@$pageFor = "snacks";
$max_results = 4;

$from = (($page * $max_results) - $max_results);
$getData = mysql_query("SELECT * FROM snacks LIMIT $from, $max_results") or die("Failed to Query the Snacks table");
//$getData = mysql_query("SELECT * FROM snacks") or die("Failed to Query the Snacks table");
$_SESSION['hG'] = $Clicked="snacks";
$stuff = "Snacks";
$G = "Snack";
$_SESSION['G'] = "snacks";

if(!mysql_num_rows($getData)>0){

$msg =  "No data found, Snacks table Empty";
header("location:dataColl.php?poll=$msg");
//header("location: dataColl.php?poll=$msg");
//include "dataColl.php";
}else{
$msg =  "Some data found";
//header("location: dataColl.php?poll=$msg");
include "dataColl.php";
}

}

if (isset($_GET['page'])  and $_SESSION['G'] == "services"){
$page = $_GET['page'];
showServices();
 }
 
function showServices(){

if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; 

@$pageFor = "services";
$max_results = 4;

$from = (($page * $max_results) - $max_results);
$getData = mysql_query("SELECT * FROM services LIMIT $from, $max_results") or die("Failed to Query the Services table");

//$getData = mysql_query("SELECT * FROM services") or die("Failed to Query the Food table");
$_SESSION['hG'] = $Clicked="services";

$stuff = "Services";
$G = "Service";
$_SESSION['G'] = "services";

if(!mysql_num_rows($getData)>0){
$msg =  "No data found, Services table Empty";
header("location:dataColl.php?poll=$msg");
//header("location: dataColl.php?poll=$msg");
//include "dataColl.php";
}else{
$msg =  "Some data found";
//header("location: dataColl.php?poll=$msg");
include "dataColl.php";
}
  }

?>